<template>
  <uni-nav-bar title="账户明细" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package" style="padding:10rpx 20rpx;">
    <view class="datas">
      <uni-datetime-picker v-model="range" type="daterange" :border="false" :clear-icon="false" rangeSeparator="至"
        @change="change" />
    </view>
  </view>
  <view class="package">{{TeamInfo.station.name}}</view>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px'}">
    <view class="package" v-for="(item,index) in orderLists" :key="item.id"
      :style="{padding:'0px 20rpx',marginTop:index===0?'0px':'15rpx'}">
      <view class="lists">
        <view class="space-between">
          <view>{{item.memo}}</view>
          <view>{{item.money}}</view>
        </view>
        <view class="space-between" style="margin-top:5rpx;">
          <view>{{item.createtime}}</view>
          <view>{{item.driver_name}}</view>
        </view>
      </view>
    </view>
    <view v-if="orderLists.length<1" class="column" style="height:50vh;">
      <image src="https://wx.anlan.tech/uploads/20211216/1ac84501c52b18f0a36262512674dae2.png" mode=""
        style="width:340rpx;height:300rpx;"></image>
      <view style="margin-top:20rpx;color:#666;">
        暂无明细
      </view>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
  import { navBack } from '@/utils/navigator';
  import { ref } from 'vue';
  import { toPublish } from '@mqtt';
  import { getDeliveryMoneyLog } from '@/gql/administrator';
  import { showLoading } from '@/utils/prompt';
  import { format, subDays } from 'date-fns';
  import { information } from '@/stores/administrator';
  const TeamInfo = information();
  // data
  const now = new Date();
  let end = format(now, 'yyyy-MM-dd');
  const daysAgo = subDays(now, 6);
  let start = format(daysAgo, 'yyyy-MM-dd');
  const range = ref([start, end]);

  const isLoading = ref(false);
  const lastPage = ref(1);
  const currentPage = ref(1);
  const orderLists = ref([]);

  const pageHeight = ref()
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = (res.windowHeight - 160).toString();
    },
  });
  init()
  function init() {
    showLoading()
    isLoading.value = true;
    const payload = {
      query: getDeliveryMoneyLog,
      variables: {
        page: currentPage.value,
        start,
        end,
        delivery_id: Number(TeamInfo.station.id)
      },
    };
    toPublish(
      'ql/control/getDeliveryMoneyLog',
      payload,
      (obj : any) => {
        const { getDeliveryMoneyLog } = obj.data;
        isLoading.value = false;
        orderLists.value = [...orderLists.value, ...getDeliveryMoneyLog.list];
        lastPage.value = getDeliveryMoneyLog.lastpage;
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      init();
    }
  }
  /**
   * 时间筛选
   * @param {String} sta 开始时间
   * @param {String} en 结束时间
   */
  function change([sta, en] : any) {
    currentPage.value = 1;
    orderLists.value = [];
    start = sta;
    end = en;
    init();
  }
</script>

<style scoped lang="less">
  .lists {
    width: 100%;
    padding: 20rpx 0px;
    border-bottom: 1px solid #eee;
  }
</style>
<style>
  .datas>>>.uni-date__x-input {
    font-size: 30rpx !important;
  }

  .datas>>>.uni-date-x {
    font-size: 30rpx !important;
  }

  .datas>>>.uni-icons {
    font-size: 48rpx !important;
  }
</style>